﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Security.Cryptography;
using System.Runtime.InteropServices;

namespace LEDShow.Helpers
{
    public class CONSTDATA
    {
        public const string IP = "127.0.0.1"; // 默认远程ip地址

        public const int Port = 8888;

        public const int Driver_Tips_Interval = 1000 * 10 * 1;

        public const int UpdateInfoThreadSleepTime = 1000 * 60;

        public static int UpdateTrainIDSleepTime = 1000 * 20;

        public static int UpdateNotifySleepTime = 1000 * 30 ;

        public static int UpdateHeadImageSleepTime = 1000 * 60 * 20;

        public static int LaterNoRemind = 10;

        public static string SourcePath = "";  //远程TomCat网址

        public static string NativePath = ""; //本地头像地址
        /// <summary>
        /// 显示铭牌的个数
        /// </summary>
        public const int Number = 6;

        /// <summary>
        /// 车场人员名牌显示
        /// </summary>
        public const int DriverNumber = 4;

        ///// <summary>
        ///// 通知颜色:紧急为黑色，重要为绿色，正常为黑色
        ///// </summary>
        //public const System.Windows.Media.Brushes[] NotifyColors = { System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.Green, System.Windows.Media.Brushes.Red };

        /// <summary>
        /// 是否启动更新天气的线程
        /// </summary>
        public static bool IsUpdateWeather = false;

        public static int UpdateWeatherWebSeevicTime = 10 * 1000;

        public static int UpdateWeatherTime = 10 * 1000;

        public static string CityName = "沈阳";

        /// <summary>
        /// 出勤提醒字幕滚动时间
        /// </summary>
        public static int AttendanceRemind = 7;

        /// <summary>
        /// 显示即将出勤的时间
        /// </summary>
        public static int ShowNextDutyTime = 60 * 10000;
       


    }

    public static class GlobalVariables
    {
        public static bool IsNumberic(string _string)
        {
            int a = 0;
            return int.TryParse(_string, out a);
        }

        public static void GetLineTwoTrain(string tbname, ref ArrayList TrainIdofTwo)
        {
            string sql = "select distinct MidID from [" + tbname + "] order by MidID ASC";
            try
            {
                TrainIdofTwo.Clear();
                SqlDataLayer traindivide = new SqlDataLayer();
                DataTable dtLineTwo = traindivide.SelectDt(sql);
                if (dtLineTwo != null)
                {
                    for (int i = 0; i < dtLineTwo.Rows.Count; i++)
                    {
                        TrainIdofTwo.Add(dtLineTwo.Rows[i]["MidID"].ToString());
                    }
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        public static string LoadHeadImage(string user_en)
        {
            if (string.IsNullOrEmpty(user_en))
                return null;

            string imageRoot = System.Windows.Forms.Application.StartupPath;
            string imagePath = imageRoot + @"\headImages\" + user_en + ".jpg";
            string defaultPath = imageRoot + @"\Images\defaultHeadImage.jpg";

            if (!Directory.Exists(imageRoot + @"\headImages\"))
            {
                Directory.CreateDirectory(imageRoot + @"\headImages\");
            }

            if (!File.Exists(imagePath))
                if (!File.Exists(defaultPath))
                    return null;
                else
                    return defaultPath;

            return imagePath;
        }

        public static StringBuilder DecryptCode(string code)
        {
            MD5 md5pwd = MD5.Create();
            byte[] criptdata = md5pwd.ComputeHash(Encoding.Default.GetBytes(code));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < criptdata.Length; i++)
            {
                sb.Append(criptdata[i].ToString("x2"));
            }
            return sb;
        }

        /// <summary>
        /// 将秒以hh:mm:ss的形式表示
        /// </summary>
        /// <param name="timestr"></param>
        /// <returns></returns>
        public static string FormatTime(string timestr)
        {
            if (timestr.Length < 1) { return null; }
           
            int getobj = Convert.ToInt32(timestr);
            if (getobj > 86400)
            {
                getobj -= 86400;
            }
            long a, b, c;
            string ret = string.Empty;
            if (getobj > 0)
            {
                a = (getobj / 3600);
                b = ((getobj % 3600) / 60);
                c = ((getobj % 3600) % 60);
                ret = a < 10 ? "0" + a.ToString() : a.ToString();
                ret += ":";
                ret += b < 10 ? "0" + b.ToString() : b.ToString();
                ret += ":";
                ret += c < 10 ? "0" + c.ToString() : c.ToString();
            }
            return ret;
        }

        /// <summary>
        /// 将hh:mm:ss的形式以秒返回
        /// </summary>
        /// <param name="strTime"></param>
        /// <returns></returns>
        public static int FormatTimeToInt(string strTime)
        {
            if (strTime == null || strTime.Length < 1) { return 0; }
            string[] timeArr = strTime.Split(':');
            int hour = Convert.ToInt32(timeArr[0]);
            int mm = Convert.ToInt32(timeArr[1]);
            int ss = Convert.ToInt32(timeArr[2]);

            return hour * 3600 + mm * 60 + ss;
        }

        /// <summary>
        /// 获取某个车次的发车时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetStartTime(string tbName,string trainId,int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select top 1 Depart from [" + tbName + "] where TrainId = '" + trainId + "' and Depart<>-1 order by Depart asc";
            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlStart);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlStart))); }
            
        }

        /// <summary>
        /// 返回提前后的时间
        /// </summary>
        /// <param name="myTime">HH:mm:ss</param>
        /// <param name="minAhead">int min</param>
        /// <returns></returns>
        public static string GetAheadTime(string myTime,int minAhead)
        {
            string result = (FormatTimeToInt(myTime) - minAhead * 60).ToString();
            return GlobalVariables.FormatTime(result);
        }

        /// <summary>
        /// 获取某个车次的结束时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="trainId">车次号</param>
        /// <returns></returns>
        public static object GetFinishTime(string tbName, string trainId,int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlFinish = "select top 1 Arrive from [" + tbName + "] where TrainId = '" + trainId + "' order by Arrive desc";

            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlFinish);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlFinish))); }
            

        }

    }

}
